package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Activity.SetAliAccountActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.weiyi.mashangzhu.Weight.PasswordEditText;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity implements View.OnClickListener, PasswordEditText.PasswordFullListener {

    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.et)
    EditText et;
    String account;
    @BindView(R.id.can)
    TextView can;
    ImageView delete, deletekeyboard;
    TextView one, two, three, four, five, six, seven, eight, nine, zero,forgot_pwd;
    PasswordEditText passwordEditText;
    String info;
    String account2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        info=getIntent().getStringExtra("info");
        account = getIntent().getStringExtra("account");
        can.setText("可提现金额"+account+"元");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x001 && data != null) {
            if (resultCode == 0x002) {
                 account2 = data.getStringExtra("account");
                Log.e("gggggggggggggggggggg","="+account2);
                if (account2!=null&&account2.length()>8) {
                    tvAccount.setText(account2.substring(0, 3) + "****" + account2.substring(7));
                }else {
                    if (account2!=null){
                        tvAccount.setText(account2);
                    }else {
                        T.showShort(WithdrawActivity.this,"账户选择失败");
                    }

                }
            }
        }
    }
    public void showPopupWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_input_keyboard, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.main_menu_animstyle);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.shape_pop_bg));
        deletekeyboard = (ImageView) contentView.findViewById(R.id.delete_dialog);
        delete = (ImageView) contentView.findViewById(R.id.delete);
        one = (TextView) contentView.findViewById(R.id.one);
        two = (TextView) contentView.findViewById(R.id.two);
        three = (TextView) contentView.findViewById(R.id.three);
        four = (TextView) contentView.findViewById(R.id.four);
        five = (TextView) contentView.findViewById(R.id.five);
        six = (TextView) contentView.findViewById(R.id.six);
        seven = (TextView) contentView.findViewById(R.id.seven);
        eight = (TextView) contentView.findViewById(R.id.eight);
        nine = (TextView) contentView.findViewById(R.id.nine);
        zero = (TextView) contentView.findViewById(R.id.zero);
        forgot_pwd = (TextView) contentView.findViewById(R.id.forgot_pwd);
        passwordEditText= (PasswordEditText) contentView.findViewById(R.id.password_edit_text);
        forgot_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //忘记密码
                Intent intent=new Intent(WithdrawActivity.this,NewPwdActivity.class);
                intent.putExtra("info","tixian_"+info);
                WithdrawActivity.this.startActivity(intent);
            }
        });

        delete.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        passwordEditText.setOnPasswordFullListener(this);
        // 设置好参数之后再show
        if (popupWindow.isShowing()){

        }else {
            popupWindow.showAsDropDown(contentView);
        }

        deletekeyboard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }
    @Override
    public void onClick(View v) {
        View clickView = v;
        if (clickView instanceof TextView) {
            // 如果点击的是TextView
            String number = ((TextView) clickView).getText().toString();
            if (!TextUtils.isEmpty(number)) {
                passwordEditText.addPassword(number);
            }
        } else if (clickView instanceof ImageView) {
            // 如果是图片那肯定点击的是删除
            passwordEditText.deleteLastPassword();
        }
    }

    @Override
    public void passwordFull(String password) {
//        Toast.makeText(this, "密码填充完毕：" + password, Toast.LENGTH_SHORT).show();

        if (account2!=null&&!account2.equals("")){
            LoadingDialog.show(WithdrawActivity.this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(this, "token", "") + "")
                    .addParams("wPrice", et.getText() + "")
                    .addParams("withAccount", account2)
                    .addParams("uAccountpw",password)
                    .url(API.BaseApi_L + API.WithDraw_Api)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            LoadingDialog.dismiss(WithdrawActivity.this);
                            T.showShort(WithdrawActivity.this, "网络连接失败");
                        }

                        @Override
                        public void onResponse(String response) {
                            LoadingDialog.dismiss(WithdrawActivity.this);
                            Log.e("wwwwwwwww", "==" + response);
                            try {
                                if (response!=null){
                                    JSONObject jsonObject = new JSONObject(response);
                                    T.showShort(WithdrawActivity.this, jsonObject.getString("msg"));
                                    if (jsonObject.getInt("status") == 0) {
                                        finish();
                                    }
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

//
    }

    @OnClick({R.id.back, R.id.account, R.id.identity_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.account:
                final Intent intent = new Intent(this, SetAliAccountActivity.class);
                startActivityForResult(intent, 0x001);
                break;
            case R.id.identity_confirm:
                if ((tvAccount.getText() + "").equals("")) {
                    T.showShort(this, "请选择支付宝账号");
                } else {
                    if ((et.getText() + "").equals("")) {
                        T.showShort(this, "请输入提现金额");
                    } else {
                        LoadingDialog.show(this);
                        OkHttpUtils.get()
                                .addParams("token",SPUtils.get(WithdrawActivity.this,"token","")+"")
                                .url(API.BaseApi_L+API.GetPwd)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(WithdrawActivity.this);
                                        T.showShort(WithdrawActivity.this,"数据获取失败，请稍后再试");

                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        LoadingDialog.dismiss(WithdrawActivity.this);
                                        Log.e("pppppppppppp","="+response);
                                        if (response!=null){
                                            try {
                                                JSONObject jsonObject=new JSONObject(response);
                                                if (jsonObject.getInt("status")==0){
                                                    if (jsonObject.getString("data")==null||jsonObject.getString("data").equals("null")||jsonObject.getString("data").equals("")){
                                                      //无密码
                                                        Intent intent1=new Intent(WithdrawActivity.this,ChangeRealPwdActivity.class);
                                                        intent1.putExtra("info","set");
                                                        WithdrawActivity.this.startActivity(intent1);
//
                                                    }else {
                                                        //有密码
                                                        showPopupWindow();
                                                    }
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }

                                    }
                                });





                    }
                }
                break;
        }
    }
}
